#include "winEdit.h"
#include "editManager.h"

winEdit::winEdit() : m_oldProc(NULL), m_newProc(NULL)
{}

winEdit::~winEdit()
{
	DestroyWindow(m_hwnd);
}

HWND
winEdit::Create(TCHAR tsname[], HWND hwnd)
{
	shape sp;
	getShape(&sp);
	m_uiMenu = editManager::Get()->increaseMenu();
	m_hWndContainer = hwnd;
	m_hwnd =  CreateWindow(TEXT("edit"),
								tsname,
								WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
								sp.m_x,
								sp.m_y,
								sp.m_cx,
								sp.m_cy,
								hwnd,
								(HMENU)m_uiMenu,
								editManager::Get()->getInstance(),
								NULL);
	return m_hwnd;
}

WNDPROC
winEdit::getOldProc()
{
	return m_oldProc;
}

WNDPROC
winEdit::getNewProc()
{
	return m_newProc;
}

int
winEdit::addWindowProc(WNDPROC newProc)
{
	m_newProc = newProc;
	m_oldProc = (WNDPROC)SetWindowLongPtr(m_hwnd, GWL_WNDPROC, (LONG)winEdit::winProcShell);
	return 0;
}

LRESULT CALLBACK
winEdit::winProcShell(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	winEdit * pEdit = editManager::Get()->getObjByHwnd(hwnd);
	if (NULL != pEdit){
		int ret = pEdit->getNewProc()(hwnd, message, wParam, lParam);
		if (1 == ret){
			ret = pEdit->getOldProc()(hwnd, message, wParam, lParam);
		}
		return ret;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}